package easamples;

import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

import org.sparx.Collection;
import org.sparx.Element;
import org.sparx.Repository;

public class RecursiveDumpExample implements IExample
{
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		return "Dump Model (Recursive)";
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.sparx.javaexample.test.IExample#getDescription()
	 */
	public String getDescription()
	{
		return "Iterate through an EAP file using recursion.<br/><br/><b>Related APIs</b><br/><ul><li><a href='http://www.sparxsystems.com.au/EAUserGuide/index.html?repository3.htm'>Repository API</a></li></ul>";
	}

	/*
	 * (non-Javadoc)
	 * @see org.sparx.javaexample.test.IExample#runExample(java.io.File, javax.swing.DefaultListModel)
	 */
	public void runExample( File projectFile, ExampleUI uiFrame )
	{
		Repository rep = null;
		DefaultListModel outputList = uiFrame.getOutputListModel();
		
		try
		{
			// Create a repository object - This will create a new instance of EA
			rep = new Repository();
			
			// Attempt to open the provided file
			if ( rep.OpenFile(projectFile.getAbsolutePath()) )
			{
				// Iterate through all models in the project
				Collection<org.sparx.Package> models = rep.GetModels();
				for ( short i = 0 ; i < models.GetCount() ; i++ )
				{
					// Recursivley process this package
					this.dumpPackage( "", models.GetAt( i ), outputList );
				}
				
				outputList.addElement( "Done!" );
			}
			else
			{
				// If the file couldn't be opened then notify the user
				String message = "EA was unable to open the file '";
				message += projectFile.getAbsolutePath();
				message += "'.";
				JOptionPane.showMessageDialog( uiFrame, message, "Invalid File", JOptionPane.ERROR_MESSAGE );
				outputList.addElement( message );
			}
		}
		catch ( UnsatisfiedLinkError ule )
		{
			String message = "Unable to communicate with EA.\nPlease ensure that the path to SSJavaCOM.dll included in java.library.path";
			JOptionPane.showMessageDialog( uiFrame, message, "Unable to link to SSJavaCOM.dll", JOptionPane.ERROR_MESSAGE );
			outputList.addElement( message );
		}
		catch ( Exception e )
		{
			String message = "An unhandled exception was thrown: " + e.getMessage();
			JOptionPane.showMessageDialog( uiFrame, message, "Unhandled Exception", JOptionPane.ERROR_MESSAGE );
			outputList.addElement( message );
		}
		finally
		{
			if ( rep != null )
			{
				// Clean up
				rep.CloseFile();
				rep.Exit();
				rep.destroy();
			}
		}
	}
	
	/**
	 * Outputs the packages name and elements, and then recursivley processes any child packages
	 * 
	 * @param indent A string representing the current level of indentation
	 * @param thePackage The package object to be processed
	 * @param outputList The DefaultListModel to output results to
	 */
	private void dumpPackage( String indent, org.sparx.Package thePackage, DefaultListModel outputList )
	{
		// Add a this package's name to the list
		outputList.addElement( indent + thePackage.GetName() + " (PackageID=" + thePackage.GetPackageID() + ")");
		
		// Dump the elements this package contains
		this.dumpElements( indent + "    ", thePackage, outputList );
		
		// Recursivley process any child packages
		Collection<org.sparx.Package> children = thePackage.GetPackages();
		for ( short i = 0 ; i < children.GetCount() ; i++ )
		{
			this.dumpPackage( indent + "    ", children.GetAt(i), outputList );
		}
	}
	
	/**
	 * Outputs the elements of the provided package to the provided DefaultListModel
	 * 
	 * @param indent A string representing the current level of indentation
	 * @param thePackage The package object to be processed
	 * @param outputList The DefaultListModel to output results to
	 */
	private void dumpElements( String indent, org.sparx.Package thePackage, DefaultListModel outputList )
	{
		// Iterate through all elements and add them to the list
		Collection<Element> elements = thePackage.GetElements();
		for ( short i = 0 ; i < elements.GetCount() ; i++ )
		{
			Element currentElement = elements.GetAt( i );
			outputList.addElement( indent + "::" + currentElement.GetName() 
										+ " (" + currentElement.GetType() 
										+ ", ID=" + currentElement.GetElementID() + ")");
		}
	}
}
